/*
 * Settings.cpp
 *
 *  Created on: 16.03.2011
 *      Author: niea
 */

#include "Settings.hpp"
#include <boost/program_options.hpp>
#include <iostream>

#include <cstdio>
#include <cstdlib>
#include <ctime>

namespace po = boost::program_options;
namespace efs
{

Settings::Settings()
	: udp_port (1234)
	, ping_period (3)
	, folder_monitor_period (1)
	, folder("test_folder_1/")
{
	my_net_settings.tcp_port = 3000;
	my_net_settings.file_port = 8080;
}

Settings::~Settings()
{}

void Settings::loadCommandLine(int ac, char* av[])
{
	po::options_description desc("Allowed options");
	        desc.add_options()
	            ("help", "produce help message")
	            ("udp-port", po::value<int>(), "set udp pinger port")
	            ("udp-period", po::value<int>(), "set udp pinger period")
	            ("tcp-port", po::value<int>(), "set tcp port")
	            ("folder", po::value<std::string>(), "set folder path")
	            ("folder-period", po::value<std::string>(), "folder monitor period")
	            ("file-port", po::value<int>(), "file server port")
	            ("random-ports", "random TCP port for messages and server each start")
	        ;

	try
	{
		po::variables_map vm;
		po::store(po::parse_command_line(ac, av, desc), vm);
		po::notify(vm);

		if (vm.count("help"))
		{
			std::cout << desc << std::endl;
		}

		if (vm.count("udp-port"))
		{
			udp_port = vm["udp-port"].as<int>();
		}

		if (vm.count("udp-period"))
		{
			ping_period = vm["udp-period"].as<int>();
		}

		if (vm.count("tcp-port"))
		{
			my_net_settings.tcp_port = vm["tcp-port"].as<int>();
		}

		if (vm.count("folder"))
		{
			folder = vm["folder"].as<std::string>();
		}

		if (vm.count("folder-period"))
		{
			folder_monitor_period = vm["folder-period"].as<int>();
		}

		if (vm.count("file-port"))
		{
			my_net_settings.file_port = vm["file-port"].as<int>();
		}

		if (vm.count("random-ports"))
		{
			srand(time(NULL));
			my_net_settings.file_port = rand() % 10000 + 1000;
			my_net_settings.tcp_port = rand() % 10000 + 1000;
		}

	}
	catch (...)
	{
		pantheios::log_ERROR("[Settings]: error parsing command line");
	}
}

void Settings::loadConfigFile(std::string const &path)
{
	//TODO: add loading
}

}
